#include <gccore.h>
#include <wiiuse/wpad.h>  
#include <fat.h>       
#include <ogc/gx.h>    
#include <wiisprite.h>


#include "Creature.h"
#include "Link.h"
#include "Foe.h"
#include "Engine.h"

#define APP_PATH "sd:/apps/PagesOfLore/"

#define IMG_PATH "sd:/apps/PagesOfLore/data/"

// libwiisprite uses wsp as it's namespace
using namespace wsp;
using namespace std;

// Called when the program would exit.  For debugging.  If there is no gamewindow, it just exits.
// Idea: Made it look like The Blue Screen of Death.
void fail_loop(GameWindow *gwd)
{
	// if(gwd==NULL) exit(0);
	
	gwd->SetBackground((GXColor){ 255, 0, 0, 255 });  //Make the screen red.
	gwd->Flush();
	
	while(42)
	{
		WPAD_ScanPads();		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME)
			exit(0);
		
		gwd->Flush();
	}
	
	exit(0);
}

int main(int argc, char **argv)
{
	// Create the game window and initalise the VIDEO subsystem
	GameWindow *gwd = new GameWindow;
	gwd->InitVideo();
	
	gwd->SetBackground((GXColor){ 0, 0xdd, 0x11, 255 });	
	
	// Initalize the filesystem
	fatInitDefault();
	
	// Initialise Wiimote
	WPAD_Init();	
	
		
		
	
	Engine eng(10);
	// Layer Manager
	LayerManager manager(3);  //Only three for now.  Make bigger when needed.
	
	// Sprites
	Link link; //Todo: Dynamically allocate
	Image linkWalkSheet;
	 
	if(linkWalkSheet.LoadImage(IMG_PATH "linkWalkSheet.png") != IMG_LOAD_ERROR_NONE){
			//fail_loop(gwd);
			exit(0);
	}
	
	link.SetImage(&linkWalkSheet, 48, 64);  //each frame is 48 across and 64 in height
	
	link.SetPosition( 100,100);
	
//	link.sprite.SetZoom(2.0f);  // Link needs to look bigger	
	
	/* Sequence order:
	 * DIR_LEFT,
	 * DIR_UP,
	 * DIR_DOWN,
	 * DIR_RIGHT
	 */
	u32 left[]	={10,9,10,11};
	u32	up[]	={1,0,1,2};
	u32	down[]	={7,6,7,8};
	u32	right[]	={4,3,4,5};
		
	link.SetSequences(left,4,up,4,down,4,right,4);
	
	link.Face(DIR_DOWN);
	
	eng.AddEntity(&link);
	//manager.Append(link.GetLayer());
	
	// Simple trap
	Foe trap;
	Image trapBlue;
	
	
	if(trapBlue.LoadImage(IMG_PATH "TrapBlue.png") != IMG_LOAD_ERROR_NONE){
			fail_loop(gwd);
	}
	
	trap.SetImage(&trapBlue);  // Take size from image
	trap.SetPosition(300,300);
	eng.AddEntity(&trap);
	//manager.Append(trap.GetLayer()); //Testing the GetLayer method of the entity
	

	while(42)
	{
		WPAD_ScanPads();		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME){
			exit(0);
		}
		
		
		
		if(WPAD_ButtonsHeld(WPAD_CHAN_0)&WPAD_BUTTON_B){  // Debug override			
			if(WPAD_ButtonsHeld(WPAD_CHAN_0)&WPAD_BUTTON_UP){
				link.Face(DIR_UP);
			}else if(WPAD_ButtonsHeld(WPAD_CHAN_0)&WPAD_BUTTON_DOWN){
				link.Face(DIR_DOWN);
			}else if(WPAD_ButtonsHeld(WPAD_CHAN_0)&WPAD_BUTTON_LEFT){
				link.Face(DIR_LEFT);
			}else if(WPAD_ButtonsHeld(WPAD_CHAN_0)&WPAD_BUTTON_RIGHT){
				link.Face(DIR_RIGHT);
			}
			
			if(WPAD_ButtonsDown(WPAD_CHAN_0)&(WPAD_BUTTON_PLUS)){
				//Reset
				link.Face(DIR_DOWN);
				link.SetPosition(100, 200);		
			}
		
			if(WPAD_ButtonsDown(WPAD_CHAN_0)&(WPAD_BUTTON_MINUS)){
				//Reset
				link.Face(DIR_LEFT);
				link.SetPosition(100, 200);		
			}
		
			if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_1){
				link.sprite.NextFrame();
			}
		
			if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_2){
				link.sprite.PrevFrame();
			}
		}else{
			eng.Update();
		}
		
		eng.layerManager.Draw(0,0);
		
		gwd->Flush();
	}
	return 0;
}


